package com.lone.smallmovie.utils.framework;

import com.lone.smallmovie.utils.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;


/**
 * 
 * 
 * <p>
 * Title: 
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * <p>
 * Company: Ant
 * </p>
 * 
 * @author Ant
 * @version 1.0
 */

public class SetCharacterEncodingFilter implements Filter{
	
	private static Log log = LogFactory.getLog(SetCharacterEncodingFilter.class);
	
	public void doFilter(ServletRequest servletRequest,ServletResponse servletResponse, FilterChain filterChain)
										throws IOException,ServletException,UnsupportedEncodingException{
		
		HttpServletResponse response =(HttpServletResponse)servletResponse;
		response.setHeader("Access-Control-Allow-Origin", "*");
		String reqPath = ((HttpServletRequest)servletRequest).getRequestURI();
		
		log.info("请求IP :" + ((HttpServletRequest)servletRequest).getRemoteAddr());
		
		log.info("请求地址:" + reqPath);
		
		//路径过滤器
		if (reqPath.startsWith("/bqTrade/apk") || reqPath.startsWith("/SmallMovie/image/headPortrait")|| reqPath.startsWith("/bqTrade/homepage/upload/")){
			filterChain.doFilter(servletRequest, response);
		}else if (CindyTools.checkPath(reqPath)){
			filterChain.doFilter(servletRequest, response);
		}else{
			Map<String, Object> message = new HashMap<String, Object>();
			//获取tokenId唯一标识
 			String tokenId = ((HttpServletRequest)servletRequest).getParameter("token");
			if (tokenId == null){
				message.put(StatusCode.STATUS, "9001");
				message.put(StatusCode.MESSAGE, "登录失败，请重新登录！");
				MessageTools.send(((HttpServletRequest)servletRequest), ((HttpServletResponse)servletResponse), message);
			}else{
				try{
					//对tokenId进行解密操作，获取需要的当前用户的登录信息。
//					JSONObject jsObj = JSONObject.fromObject(AES.AES_de(tokenId, AES.AES_KEY));
					JSONObject jsObj = JSONObject.parseObject(AES.AES_de(tokenId, AES.AES_KEY));
					log.info("jsObj:"+jsObj);
					//从session中获取当前登录用户的信息。
					System.out.println(jsObj.getString("userId"));
					Map<String, String> user = LoginSession.getMap(jsObj.getString("userId"));
					if (user != null){
						String userLoginUUIDBySession = user.get("userLoginUUID");
						String userLoginUUIDByCurrent = jsObj.getString("userLoginUUID");
						//如果当前登录的用户UUID和session中存储的UUID一样，则表示验证通过，可以访问相关链接。
						if (userLoginUUIDBySession.equals(userLoginUUIDByCurrent)){
							String userIdByCurrent =jsObj.getString("userId");
							log.info("请求用户:" + userIdByCurrent);
							((HttpServletRequest)servletRequest).getSession().setAttribute(userIdByCurrent, user);
							//将当前用户存储到seesion中
							LoginSession.addMap(userIdByCurrent, user);
							filterChain.doFilter(servletRequest, response);
						}else{
							message.put(StatusCode.STATUS, "9002");
							message.put(StatusCode.MESSAGE, "用户在其他设备登录，请重新登录！");
							MessageTools.send(((HttpServletRequest)servletRequest), ((HttpServletResponse)servletResponse), message);
						}
					}else{
						message.put(StatusCode.STATUS, "9003");
						message.put(StatusCode.MESSAGE,"登录失败，请重新登录！");
						MessageTools.send(((HttpServletRequest)servletRequest), ((HttpServletResponse)servletResponse), message);
					}
				}catch(Exception e){
					log.info(e);
					log.info("token : " + tokenId);
					message.put(StatusCode.STATUS, "9004");
					message.put(StatusCode.MESSAGE,"登录失败，请重新登录！");
					MessageTools.send(((HttpServletRequest)servletRequest), ((HttpServletResponse)servletResponse), message);
				}
			}
		}
	}
	
	public void destroy(){
		
	}

	public void init(FilterConfig filterConfig){
		
	}
}